package com.online.demo.controller;


import com.online.demo.common.CommonStr;
import com.online.demo.common.ResultBean;
import com.online.demo.entity.TUser;
import com.online.demo.service.ITUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sda1
 * @since 2020-11-24
 */
@Controller
@RequestMapping("/admin")
@Slf4j
public class TAdminController {
    @Autowired
    private ITUserService userService;
    @Autowired
    private RedisTemplate redisTemplate;


    // 注册
    @PostMapping("adminRegist")
    @ResponseBody
    public ResultBean<TUser> userRegist(String account, String telNum, String email, String password ,
                                        String province, String area, String address, String nickName,
                                        String gender, boolean userType){
        StringBuilder addr = new StringBuilder(province).append(area).append(address);

        TUser user = new TUser(account,telNum, email, password, true, LocalDateTime.now(), addr.toString(), nickName, gender, 0, "--", true);
        user.setUsertype(userType); // 0表示管理员
        userService.userRegist(user);
        return ResultBean.getSuccessInstance("注册成功..");
    }


    // 用户登录
    @PostMapping("adminLogin")
    @ResponseBody
    public ResultBean<TUser> adminLogin( String accountInfo, String password, HttpServletRequest request, HttpServletResponse response){
        TUser user = userService.userLogin(accountInfo, password);

        if(user == null){
            return ResultBean.getErrorInstance("用户不存在..");
        }

        // 构造 cookie
        String userToken = UUID.randomUUID().toString();
        Cookie cookie = new Cookie(CommonStr.COOKIE_ADMIN_TOKEN, userToken);
        cookie.setMaxAge(30); // cookie存活时间
        cookie.setPath("/");
        cookie.setHttpOnly(true);

        // redis中存储信息
        StringBuilder redisKey = new StringBuilder(CommonStr.REDIS_KEY_ADMIN).append(userToken);
        redisTemplate.opsForValue().set(redisKey.toString(), user);
        redisTemplate.expire(redisKey.toString(), 30, TimeUnit.MINUTES);

        // 将cookie返回给前端
        response.addCookie(cookie);

        ResultBean<TUser> result = ResultBean.getSuccessInstance("登录成功..");
        result.setData(user);
        return result;
    }


    // 显示个人信息
    @GetMapping("showAdminInfo")
    @ResponseBody
    public ResultBean<TUser> showUserInfo(HttpServletRequest request, Model model){
        TUser currUser = (TUser) request.getAttribute("admin");

        if(currUser == null){
            return ResultBean.getErrorInstance("您还没有登录, 不能查看个人信息..");
        }

        ResultBean<TUser> result = ResultBean.getSuccessInstance("已为您显示个人信息..");
        result.setData(currUser);
        return result;
    }

    // 修改个人信息
    @GetMapping("updateAdminInfo")
    @ResponseBody
    public ResultBean<TUser> updateUserInfo(HttpServletRequest request, String telnum, String email, String password, String address, String nickname,
                                            @CookieValue(value = CommonStr.COOKIE_ADMIN_TOKEN, required = false) String userToken){
        TUser currUser = (TUser) request.getAttribute("admin");

        if(currUser == null || StringUtils.isEmpty(userToken)){
            return ResultBean.getErrorInstance("您还没有登录,不能修改信息..");
        }

        currUser = userService.updateUserInfo(userToken, telnum, email, password, address, nickname);

        request.setAttribute("admin", currUser);
        ResultBean<TUser> result = ResultBean.getSuccessInstance("修改成功..");
        result.setData(currUser);
        return result;
    }


    // 注销
    @ResponseBody
    @GetMapping("adminLogout")
    public ResultBean<TUser> userLogout(@CookieValue(CommonStr.COOKIE_USER_TOKEN) String userToken,
                                        HttpServletRequest request, HttpServletResponse response){
        TUser currUser = (TUser) request.getAttribute("admin");

        if(currUser == null){
            return ResultBean.getErrorInstance("您还没有登录, 不能进行注销操作..");
        }

        Cookie cookie = new Cookie(CommonStr.COOKIE_ADMIN_TOKEN, "");
        cookie.setMaxAge(0);
        cookie.setHttpOnly(true);
        cookie.setPath("/");
        response.addCookie(cookie);

        StringBuilder redisKey = new StringBuilder(CommonStr.REDIS_KEY_ADMIN).append(userToken);
        redisTemplate.delete(redisKey.toString());

        log.info("logout info{}", "注销成功~");
        return ResultBean.getSuccessInstance("注销成功");
    }


    // 查询所有用户信息
    @PostMapping("showAllUserInfo")
    public ResultBean<List<TUser>> showAllUserInfo(boolean userType, HttpServletRequest request) {
        TUser currUser = (TUser) request.getAttribute("admin");

        if(currUser == null){
            return ResultBean.getErrorInstance("您没有权限..");
        }

        List<TUser> userList = userService.showAllUser(userType);
        ResultBean<List<TUser>> result = ResultBean.getSuccessInstance("用户列表查询成功..");
        result.setData(userList);
        return result;
    }

    // 查看用户信息, 包括管理员的
    @GetMapping("showUserInfo")
    public ResultBean<TUser> showUserInfo(int userId, HttpServletRequest request) {
        TUser currUser = (TUser) request.getAttribute("admin");

        if(currUser == null){
            return ResultBean.getErrorInstance("您没有权限..");
        }

        TUser user = userService.getById(userId);
        ResultBean<TUser> result = ResultBean.getSuccessInstance("查询成功..");
        result.setData(user);
        return result;

    }

    // 修改用户信息  -- 管理员只能修改密码
    @PostMapping("updateUserPassword")
    public ResultBean<TUser> updateUserPassword(int userId, String password, HttpServletRequest request) {
        TUser currUser = (TUser) request.getAttribute("admin");

        if(currUser == null){
            return ResultBean.getErrorInstance("您没有权限..");
        }

        TUser user = userService.updateUserPassword(userId, password);
        ResultBean<TUser> result = ResultBean.getSuccessInstance("修改成功..");
        result.setData(user);
        return result;
    }

    // 修改用户状态 -- 是否可以登录
    @GetMapping("updateUserStatus")
    public ResultBean<TUser> updateUserStatus(int userId, HttpServletRequest request) {
        TUser currUser = (TUser) request.getAttribute("admin");

        if(currUser == null){
            return ResultBean.getErrorInstance("您没有权限..");
        }

        TUser user = userService.updateUserStatus(userId);
        ResultBean<TUser> result = ResultBean.getSuccessInstance("修改成功..");
        result.setData(user);
        return result;

    }
}
